package com.nine.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nine.entity.UsersWithBLOBs;
import com.nine.service.AccountService;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nine.dao.AccountMapper;
import com.nine.dao.BillsMapper;
import com.nine.entity.Bills;
import com.nine.service.AccountService;

@Controller
public class AccountController {
	@Autowired
	AccountService accountService;

	@Autowired
	AccountMapper accountMapper;

	@Autowired
	BillsMapper billsMapper;

	String loginUseridSession = "username";// 登录用户ID的Session名称

	public int getLoginUserid() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		if (session.getAttribute(loginUseridSession) == null) {
			return 7;
		} else {
			String username = (String) session.getAttribute(loginUseridSession);
			return accountMapper.bank1SelectUserIdByUsername(username);
		}
	}

	public String getLoginUsername() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		if (session.getAttribute(loginUseridSession) == null) {
			return "hb";
		} else {
			return (String) session.getAttribute(loginUseridSession);
		}
	}

	// 获取登录用户ID（信息）， 进入汇款表单填写页面。
	@RequestMapping("RemittancePage.do")
	public String remittancePage(HttpSession session) {
		// 获取登录用户ID————start
		int id = 7;// 设置固定用户ID，整合后删除
//		session.getAttribute("");
		session.setAttribute("loginUserId_HB", id);
		// 获取登录用户ID————end

		return "m_bank/remittancePage.jsp";
	}

	// 表单提交进行的操作
	@RequestMapping("remittanceForm.do")
	public String remittanceForm(HttpSession session, Model model, float moneyin, String keywords) {
		int id = (int) session.getAttribute("loginUserId_HB");
		String bid = accountService.paymentOfRemittances(id, moneyin, keywords);
		model.addAttribute("bid", bid);
		return "m_bank/PaymentMethodPage.jsp";
	}

	@ResponseBody
	@RequestMapping("PaymentVerification.do")
	public int PaymentVerification(String bid) {

		return accountService.saveBillsStateAndAccountMoney(bid);
	}

	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写

	// 验证密码后操作
	@RequestMapping("bank1_1.do")
	public String bank1_1(float money, String keywords) {
		int id = getLoginUserid();
		accountService.bankHuiKuanChongZhi(id, money, keywords);
		return "bank2_1.do";
	}

	// 验证密码
	@ResponseBody
	@RequestMapping("bank1_2.do")
	public String bank1_2(int paycode) {
		int id = getLoginUserid();
		return Integer.toString(accountService.bank1SelectPaycodeById(id, paycode));
	}

	// 跳转基本账户页面
	@RequestMapping("bank2_1.do")
	public String bank2_1(Model model, @RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize) {
		int id = getLoginUserid();
		String username = accountMapper.bank1SelectUsernameByID(id);
		PageHelper.startPage(pageNum, pageSize);
		List<Bills> list = accountService.bank1SelectBillsByUsernameAndCZ(username);
		PageInfo<Bills> pageInfo = new PageInfo<Bills>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "m_bank/bank2_1.jsp";
	}

	// 内部转账
	@RequestMapping("bank3_1.do")
	public String bank3_1(Model model, HttpSession session) {
		String username = getLoginUsername();
		float money = accountMapper.bank1selectMoneyByUsername(username);
		model.addAttribute("money", money);
		return "m_bank/bank3_1.jsp";
	}

	// 验证会员
	@ResponseBody
	@RequestMapping("bank3_2.do")
	public int bank3_2(String rid) {
		return accountMapper.bank1SelectUserIsExistence(rid);
	}

	// 验证密码
	@ResponseBody
	@RequestMapping("bank3_3.do")
	public int bank3_3(int paycode) {
		int id = getLoginUserid();
		return accountService.bank1SelectPaycode(id, paycode);
	}

	// 验证密码成功，转账操作数据库
	@RequestMapping("bank3_4.do")
	public String bank3_4(String rid, float moneyout) {
		int id=getLoginUserid();
		accountService.bank1NeiBuZhuanZhang(id, rid, moneyout);
		return "bank4_1.do";
	}
	
	// 跳转基本账户页面
	@RequestMapping("bank4_1.do")
	public String bank4_1(Model model, @RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize) {
		int id = getLoginUserid();
		String username = accountMapper.bank1SelectUsernameByID(id);
		PageHelper.startPage(pageNum, pageSize);
		List<Bills> list = accountService.bank1SelectBillsByUsernameAndZZ(username);
		PageInfo<Bills> pageInfo = new PageInfo<Bills>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "m_bank/bank4_1.jsp";
	}
	
	//000
	@ResponseBody
	@RequestMapping("PayCodeByUsername")
	public Integer updatePayCodeByUsername(String paycode,HttpSession session) {
		if(paycode!=null) {
			String username=(String)session.getAttribute("username");
			if(username!=null) {
				return accountService.updatePayCodeByUsername(username, paycode);
			}
			
		}
		return null;
	}
	
	//根据用户名查找支付密码是否正确
		@RequestMapping("updatPayCodeByUsername.do")
		@ResponseBody
		public Integer updatPayCodeByUsername(HttpSession session,String payCode) {
			String username=(String)session.getAttribute("username");
			if(username!=null) {
				String payCode2 = accountService.getpayCodeByUname(username);
				 System.out.println(payCode2);   
				 if(payCode2.equals(payCode)) {
					 return 1;   
				 }
			}
			return 0;
		}
}
